package com.yx.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.system.dto.OrderDto;
import com.yx.system.entity.TdBook;
import com.yx.system.entity.TdOrder;
import com.yx.system.entity.TdSection;
import com.yx.system.service.TdBookService;
import com.yx.system.service.TdOrderService;
import com.yx.system.service.TdSectionService;
import com.yx.utils.AmountUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/** 后台管理订单controller
 * Created by wuye on 2017/12/26.
 */
@RestController
@RequestMapping("/sys/order")
public class TdOrderController2 {

    @Autowired
    private TdOrderService tdOrderService;

    @Autowired
    private TdBookService tdBookService;

    @Autowired
    private TdSectionService tdSectionService;

    /**
     * 查询所有订单列表
     * @return
     */
    @GetMapping("findAll")
    @RequiresPermissions("sys:order:list")
    public R findAll(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        Page<TdOrder> pageUtil = new Page<TdOrder>(query.getPage(), query.getLimit());
        Page<TdOrder> page = tdOrderService.queryPageList(pageUtil, query);
        return R.ok().put("page", page);
    }

    @RequestMapping("/findById/{id}")
    @RequiresPermissions("sys:order:findById")
    public R findById(@PathVariable("id") int id) throws Exception {
        TdOrder tdOrder = tdOrderService.selectByPrimaryKey(id);
        TdBook tdBook = tdBookService.selectByPrimaryKey(tdOrder.getBookid());
        TdSection tdSection = tdSectionService.selectByPrimaryKey(tdOrder.getSectionid());
        OrderDto dto = new OrderDto();
        dto.setTdBook(tdBook);
        dto.setTdSection(tdSection);
        dto.setTdOrder(tdOrder);
        return R.ok().put("dto",dto);
    }

}
